#include "default.h"
/*
unsigned int EEPROMReadInt(unsigned int eeprom_addr)
{
	unsigned int temp;
	
	temp = EEPROMread(eeprom_addr+1);
	temp = (temp<<8) + EEPROMread(eeprom_addr);
	return temp;
}
*/
unsigned int EEPROMReadInt(unsigned int eeprom_addr)
{
#ifdef ASM
    asm("rcall push_gset2");
	 
	asm("movw R20,R16"); 	  // R21:R20 =eeprom_addr
	asm("subi R16,255");	  // ; eeprom_addr+1
	asm("sbci R17,255");  
	asm("rcall _EEPROMread");
	asm("mov R22,R16");
	asm("movw R16,R20");
	asm("rcall _EEPROMread"); 
	asm("mov R17,R22");

	asm("rcall pop_gset2");
#else
	unsigned int temp;
	
	temp = EEPROMread(eeprom_addr+1);
	temp = (temp<<8) + EEPROMread(eeprom_addr);
	return temp;
#endif	
}
/*
void EEPROMReadBytes(int addr, void *ptr, int size)
{
	char *dst = ptr;

	while (size--)
	{
	 	*dst = EEPROMread(addr);
		addr++;
		dst++;
	}
}
*/
/*
unsigned char EEPROMread(unsigned int eeprom_addr)
{
	EEAR = eeprom_addr;
    EECR |= 0x01;                       // Set READ strobe
    return (EEDR);                      // Return byte
}	
*/
/*
void EEPROMwriteInt(unsigned int eeprom_addr, unsigned int data)
{
 	EEPROMwrite(eeprom_addr, data);
	EEPROMwrite(eeprom_addr+1, data>>8);
}
*/
void EEPROMwriteInt(unsigned int eeprom_addr, unsigned int data)
{
#ifdef ASM
    asm("rcall push_gset2");
	 
	asm("movw R20,R16"); 	  // R21:R20 =eeprom_addr
	asm("mov R22,R19"); 	  // R22 =data>>8
	asm("rcall _EEPROMwrite");
	asm("movw R16,R20");
	asm("subi R16,255");	  // ; eeprom_addr+1
	asm("sbci R17,255");
	asm("mov R18,R22");
	asm("rcall _EEPROMwrite");

	asm("rcall pop_gset2");
#else
 	EEPROMwrite(eeprom_addr, data);
	EEPROMwrite(eeprom_addr+1, data>>8);
#endif	
}
/*
void EEPROMWriteBytes(int addr, void *ptr, int size)
{
	char *src = ptr;

	while (size--)
	{
		EEPROMwrite(addr, *src);
		addr++;
		src++;
	}
}
*/
void EEPROMwrite(unsigned int eeprom_addr, unsigned char byte)
{

	EEAR = eeprom_addr;
    EEDR = byte;

	CLI();
	EECR |= (1<<EEMWE);                       // Set MASTER WRITE enable
	EECR |= (1<<EEWE);                       // Set MASTER WRITE enable
	SEI();
	while (EECR & (1<<EEWE));                // Wait until write is done
}

/*
void EEPROMWriteBytes(int addr, unsigned char *byte)   // int size = R21:R20
  {
    //asm("rcall push_gset2");
	asm("mov R30,R18");
	asm("mov R31,R19");
	asm("EEPROMWriteBytes_Loop1:");
	asm("ld	 R18,Z+");
	asm("out 0x1e,R16");		   // EEAR = addr;EEARL
	asm("out 0x1f,R17");		   // EEAR = addr;EEARH
	asm("out 0x1d,R18");		   // EEDR = *byte;
	CLI();
	EECR |= (1<<EEMWE);                       // Set MASTER WRITE enable
	EECR |= (1<<EEWE);                       // Set MASTER WRITE enable
	SEI();
	while (EECR & (1<<EEWE));                // Wait until write is done
	asm("subi R16,0xff");					 // addr++
	//asm("sbci R17,0xff");					 
	asm("subi R20,1");						 // size--
	//asm("sbci R21,0");
	asm("brne EEPROMWriteBytes_Loop1");
	asm("");

	//asm("rcall pop_gset2");
  }
  

void EEPROMReadBytes(int addr, void *ptr)    // R21:R20 = int size
	{
	asm("mov  R22,R16");
	asm("mov  R23,R17");
	asm("EEPROMReadBytes_Loop1:");
	asm("rcall _EEPROMread");
	asm("mov R30,R18");
	asm("mov R31,R19");
	asm("std z+0,R16");
	asm("subi R18,255");  //ptr+1
	asm("sbci R19,255");
	asm("subi R22,255");  //addr+1
	asm("sbci R23,255");
	asm("mov  R16,R22");
	asm("mov  R17,R23");
	
	asm("subi R20,1");    // size--
	asm("sbci R21,0");
	asm("tst  R20");
	asm("brne EEPROMReadBytes_Loop1");
	asm("tst  R21");
	asm("brne EEPROMReadBytes_Loop1");
	
	}
   	
*/